<?php
include("../include/db.php");
include("includes/thumbnail.php");

$_SESSION['rpage'] = $_SERVER['REQUEST_URI']; 
if(!isset($_SESSION[$prefix."_username"])){header("location:../login/");exit;}
if ($_SESSION[$prefix."_security"] < 10){header("location:../login/?error=2"); exit;}

$year = date("Y");
$date = "03/18/2011";

if ($_SERVER['REQUEST_METHOD'] == "POST") { 

	$id			= $_POST[id];
	$name		= $_POST[name];
	$date		= $_POST[date];
	$hour		= $_POST[hour];
	$min		= $_POST[min];
	$ampm		= $_POST[ampm];
	$venue		= $_POST[venue];
	$desc		= $_POST[editor1];
	$delete		= $_POST[delete];
	
if($delete == "DELETE"){

	//************************ UPDATE USERS ********************************  
	$query = mysql_query("DELETE FROM screenings WHERE screen_id = '$id'", $db);
	//********************************************************************** 

header("Location: event_list.php?action=deleted"); exit; 
}	

	
	if($ampm == 'pm'){$hour = $hour + 12;}
	$time = "00:".$hour.":".$min;
	
	$date = date("Y-m-d", strtotime($date));

	if (($_FILES['mug_file']['name']) != ""){
		$site_pic_size = 400;
		$temp_loc = $_FILES['mug_file']['tmp_name'];
		$tn_name = basename($_FILES['mug_file']['name']);
		$file = "../files/$tn_name";
	$thumbnail = global_resize($temp_loc, $site_pic_size, $tn_name, $file);
		$thumb_sql = " , screen_thumb='$thumbnail' ";
	}


	//************************ INSERT INTO COURSES_TRM ********************************  
	$query = "UPDATE screenings SET 
				event_title='$name',
				event_desc='$desc',
				date='$date',
				time='$time',
				venue_id='$venue'
				$thumb_sql
			WHERE screen_id = '$id'";
	$result = mysql_query($query, $db) or die(mysql_error());
	//********************************************************************************* 
header("Location: event_list.php?action=updated"); exit; 
}//END POST


$in_event = (int)$_GET['id'];

$result = mysql_query("SELECT * FROM screenings WHERE screen_id = '$in_event' AND type='e'  ", $db);
$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
$time = date("g:i a", strtotime(substr($myrow[time], 3)));

$time_arr = explode(":", $time);
	$hour = $time_arr[0];
	$min = substr($time_arr[1],0, 2);
	$ampm = substr($time_arr[1], 3);
	

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="includes/admin.css"/>
<title><?php echo $global_title;?></title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type="text/javascript" src="../../ckeditor/ckeditor.js"></script>
<link rel="stylesheet" type="text/css" href="includes/jqueryui.css">
<script> 
	$(function() {
		$( "#date" ).datepicker();
	});
	</script> 

</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="main"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_main.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Edit Event</h1>

	<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"  >
	<input type="hidden" id="id" name="id" value="<?php echo $in_event;?>">
		<fieldset><legend>Edit Event</legend>

          <dl>
            <dt>Name:</dt>
            <dd><input type="text" name="name" id="name" required autofocus size="50" value="<?php echo $myrow[event_title];?>" /></dd>
          </dl>
              
          <dl>
            <dt>Date:</dt>
            <dd><input type="date" name="date" id="date" value="<?php echo date("m/d/Y", strtotime($myrow[date]));?>"   /></dd>
          </dl>
          
          <dl>
            <dt>Time:</dt>
            <dd><input type="number" name="hour" id="hour" step="1" min="1" max="12" placeholder="hour" size="4" maxlength="2" value="<?php echo $hour;?>" /> :
            	<input type="number" name="min" id="min" step="1" min="0" max="60" placeholder="min" size="4" maxlength="2" value="<?php echo $min;?>"   />
            	<select name="ampm" id="ampm" >
            		<option value="am" <?php if($ampm == 'am'){echo " selected ";}?> >AM</option>
            		<option value="pm" <?php if($ampm == 'pm'){echo " selected ";}?> >PM</option>
            	</select>
            	</dd>
          </dl>
          <dl>
            <dt>Venue:</dt>
            <dd><select name="venue" id="venue" >

	<?php
             $result = mysql_query("SELECT * FROM venues ORDER BY venue_name", $db);
                while ($myrow1 = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                  <option value="<?php echo $myrow1["venue_id"];?>" <?php if($myrow1["venue_id"] == $myrow[venue_id]){echo "selected";}?>><?php echo $myrow1["venue_name"];?></option>

                    	<?php }?>

                        </select>
                    </dd>
          </dl>
           <?php if($myrow[thumbnail] != ''){ ?>
          
          <dl>
            <dt>Current Thumbnail:</dt>
            <dd><img src="<?php echo $myrow[screen_thumb];?>"></dd>
          </dl>
          
          <?php } ?>
          
          <dl>
            <dt>Event Thumbnail Upload:</dt>
            <dd><input type="file" name="mug_file" id="mug_file"  /></dd>
          </dl>
          
          
          <dl>
            <dt>Delete Event:</dt>
            <dd><input type="checkbox" name="delete" value="DELETE"></dd>
          </dl>
          
         <h3>Event Description:</h3>
            <textarea name="editor1" id="editor1" class="ckeditor"><?php echo $myrow[event_desc];?></textarea>  
                   
      <p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Update" /></p>
</fieldset>
    </form>	
		


    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->

   <script type="text/javascript">
	CKEDITOR.replace( 'editor1', {toolbar : 'MyToolbar'} );
	
	CKEDITOR.config.width = '700';
	CKEDITOR.config.height = '200';
	CKEDITOR.config.toolbarStartupExpanded = 'false';
	
</script>

</html>